﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Chefbook.DataObjects;
using NUnit.Framework;
using Rhino.Mocks;
using Chefbook.DataAccessLayer;
using Chefbook.UserActions;

namespace UnitTests.UserActionsTests
{
    [TestFixture]
    public class CreateRecipeActionFixture
    {
        private MockRepository mocks;
        private IUserAction action;
        private Category category;
        private Recipe recipe;
        private ChefbookEntities db;

        [SetUp]
        public void Init()
        {
            mocks = new MockRepository();
            db = new ChefbookEntities();
            db.ClearDatabase();

            var createCategory = new CreateCategoryAction(null, "test category");
            createCategory.DoAction();

            category = db.Categories.First();
            Assert.AreEqual(1, db.Categories.Count());
            Assert.AreEqual("test category", category.Name);
            Assert.AreEqual(0, category.DirectRecipes.Count);
            action = new CreateRecipeAction(category) { DatabaseConnector = db };
        }

        [Test]
        public void DoActionTest()
        {
            action.DoAction();
            Assert.AreEqual(1, category.DirectRecipes.Count);
            var categoryagain = db.Categories.First();
            Assert.AreEqual(category, categoryagain);
            var newRecipe = category.DirectRecipes.First(x => x != recipe);
            Assert.Contains(category, newRecipe.Categories.ToList());
            //Assert.Contains(newRecipe, db.DirectRecipes);
        }

        [Test]
        public void UndoActionTest()
        {
            action.DoAction();
            action.UndoAction();
            category = db.Categories.First();
            Assert.AreEqual(1, db.Categories.Count());
            Assert.AreEqual("test category", category.Name);
            Assert.AreEqual(0, category.DirectRecipes.Count);
            //Assert.AreEqual(0, db.DirectRecipes.Count());
        }

        [Test]
        public void UndoActionWithoutDoingTest()
        {
            UserActionTestsHelper.UndoActionWithoutDoingTest(action);
        }

        [Test]
        public void UndoActionTwiceAfterDoingTest()
        {
            UserActionTestsHelper.UndoActionTwiceAfterDoingTest(action);
        }

        [Test]
        public void DoActionTwiceTest()
        {
            UserActionTestsHelper.DoActionTwiceTest(action);
        }

        [Test]
        public void ActionNameTest()
        {
            Assert.AreEqual(string.Format("Create new Recipe in Category '{0}'", category), action.ToString());
        }
    }
}
